<template>
  <div>
    <el-form :inline="true">
      <el-form-item>
        <el-input
            v-model="searchForm.name"
            placeholder="名称"
            clearable
        >
        </el-input>
      </el-form-item>

      <el-form-item>
        <el-button @click="getLexiconList">搜索</el-button>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-circle-plus-outline" @click="dialogVisible = true">新增</el-button>
      </el-form-item>
      <el-form-item>
        <el-popconfirm title="这是确定批量删除吗？" @confirm="delHandle(null)">
          <el-button type="danger" slot="reference" :disabled="delBtlStatu">批量删除</el-button>
        </el-popconfirm>
      </el-form-item>
      <el-form-item style="float: right!important;">
        <el-button type="info" @click="refresh" icon="el-icon-refresh"></el-button>
      </el-form-item>

    </el-form>

    <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        border
        stripe
        @selection-change="handleSelectionChange">

      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column
          prop="id"
          label="词条编号"
          width="120">
      </el-table-column>

      <el-table-column
          prop="name"
          label="词条名称"
          width="120">
      </el-table-column>

      <el-table-column
          prop="user.username"
          label="创建人"
          width="120">
      </el-table-column>

      <el-table-column
          prop="created"
          width="200"
          label="创建时间"
      >
      </el-table-column>
      <el-table-column
          prop="updated"
          width="200"
          label="更新时间"
      >
      </el-table-column>
      <el-table-column
          prop="statu"
          label="状态">
        <template slot-scope="scope">
          <el-tag size="small" v-if="scope.row.statu === 1" type="success">正常</el-tag>
          <el-tag size="small" v-else-if="scope.row.statu === 0" type="danger">禁用</el-tag>
        </template>

      </el-table-column>

<!--      <el-table-column-->
<!--          prop="remark"-->
<!--          label="描述"-->
<!--          show-overflow-tooltip>-->
<!--      </el-table-column>-->

      <el-table-column
          label="操作">

        <template slot-scope="scope">
          <el-button type="primary"  icon="el-icon-view" @click="findAngleByLexId(scope.row.id)" circle></el-button>
          <el-button type="primary"  icon="el-icon-edit" @click="editHandle(scope.row.id)" circle></el-button>
          <el-divider direction="vertical"></el-divider>

          <template>
            <el-popconfirm title="这是一段内容确定删除吗？" @confirm="delHandle(scope.row.id)">
              <el-button type="danger" slot="reference" icon="el-icon-delete" circle></el-button>
            </el-popconfirm>
          </template>

        </template>
      </el-table-column>

    </el-table>


    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes, prev, pager, next, jumper"
        :page-sizes="[5, 10, 20, 30]"
        :current-page="current"
        :page-size="size"
        :total="total">
    </el-pagination>


    <!--新增对话框-->
    <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="600px"
        :before-close="handleClose">

      <el-form :model="editForm" :rules="editFormRules" ref="editForm" label-width="100px" class="demo-editForm">

        <el-form-item label="词条名称" prop="name" label-width="100px">
          <el-input v-model="editForm.name" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="状态" prop="statu" label-width="100px">
          <el-radio-group v-model="editForm.statu">
            <el-radio :label=0>禁用</el-radio>
            <el-radio :label=1>正常</el-radio>
          </el-radio-group>
        </el-form-item>



        <el-form-item label="描述" prop="remark" label-width="100px">
          <el-input v-model="editForm.remark" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="分类标签" prop="dynamicTags" label-width="100px">
          <el-tag
              :key="tag"
              v-for="tag in dynamicTags"
              closable
              :disable-transitions="false"
              @close="handleTagClose(tag)">
            {{tag}}
          </el-tag>
          <el-input
              class="input-new-tag"
              v-if="inputVisible"
              v-model="inputValue"
              ref="saveTagInput"
              size="small"
              @keyup.enter.native="handleInputConfirm"
              @blur="handleInputConfirm"
          >
          </el-input>
          <el-button v-else class="button-new-tag" size="small" @click="showInput">+ New Tag</el-button>

        </el-form-item>



        <el-form-item>
          <el-button type="primary" @click="submitForm('editForm')">确定</el-button>
          <el-button @click="resetForm('editForm')">取消</el-button>
        </el-form-item>
      </el-form>

    </el-dialog>

    <el-dialog title="已关联角度" :visible.sync="outerVisible">
      <el-button type="primary" @click="findAngleNoLexId">新建关联</el-button>
      <el-table
          ref="multipleTable"
          :data="angleData"
          tooltip-effect="dark"
          style="width: 100%"
          border
          stripe
          @selection-change="handleSelectionChange">
        <input type="hidden" v-model="angleEditForm.lexId">

        <el-table-column
            type="selection"
            width="55">
        </el-table-column>
        <el-table-column
            prop="id"
            label="角度编号"
            width="120">
        </el-table-column>

        <el-table-column
            prop="name"
            label="角度名称"
            width="120">
        </el-table-column>
        <el-table-column
            label="操作">
          <template slot-scope="scope">
            <template>
              <el-popconfirm title="是否删除关联角度及对应内容？" @confirm="delHandleAngle(scope.row.id)">
                <el-button type="danger" slot="reference" icon="el-icon-delete" circle></el-button>
              </el-popconfirm>
            </template>
          </template>
        </el-table-column>


      </el-table>
      <el-pagination
          @size-change="handleSizeChange2"
          @current-change="handleCurrentChange2"
          layout="total, prev, pager, next, jumper"
          :current-page="angleEditForm.current"
          :page-size="angleEditForm.size"
          :total="angleEditForm.total">
      </el-pagination>
      <div style="margin-top: 20px" slot="footer" class="dialog-footer">
        <el-button @click="outerVisible = false">取 消</el-button>
      </div>

      <el-dialog
          width="45%"
          title="可关联角度"
          :visible.sync="innerVisible"
          append-to-body>
        <el-table
            ref="multipleTable"
            :data="angleData2"
            tooltip-effect="dark"
            style="width: 100%"
            border
            stripe
            @selection-change="handleSelectionChange">

          <el-table-column
              type="selection"
              width="55">
          </el-table-column>

          <el-table-column
              prop="id"
              label="角度编号"
              width="120">
          </el-table-column>

          <el-table-column
              prop="name"
              label="角度名称"
              width="150">
          </el-table-column>

        </el-table>
        <el-pagination
            @current-change="handleCurrentChange3"
            layout="total,pager,next"
            :current-page="angleEditForm2.current"
            :page-size="angleEditForm2.size"
            :total="angleEditForm2.total">
        </el-pagination>
        <div style="margin-top: 20px" slot="footer" class="dialog-footer">
          <el-button type="primary" :disabled="delBtlStatu" @click="submitAngle">确认关联</el-button>
          <el-button @click="innerVisibleClose">取 消</el-button>
        </div>


      </el-dialog>

    </el-dialog>



  </div>
</template>

<script>
export default {
  name: "Lexicon",
  data() {
    return {
      dynamicTags: [],
      inputVisible: false,
      inputValue: '',
      searchForm: {},
      delBtlStatu: true,

      total: 0,
      size: 5,
      current: 1,

      dialogVisible: false,
      editForm: {

      },

      tableData: [],

      editFormRules: {
        name: [
          {required: true, message: '请输入分类名称', trigger: 'blur'}
        ],
        statu: [
          {required: true, message: '请选择状态', trigger: 'blur'}
        ]
      },

      multipleSelection: [],
      outerVisible:false,
      innerVisible:false,
      angleData:[],
      angleData2:[],
      angleEditForm:{

      },
      angleEditForm2:{

      }



    }
  },
  created() {
    this.getLexiconList()
  },
  methods: {

    innerVisibleClose(){
      this.innerVisible = false
      this.findAngleByLexId(this.angleEditForm.lexId)
    },

    delHandleAngle(aid){
      var contents = []
      var content = {lexId:this.angleEditForm.lexId,aid:aid}
      contents.push(content)
      this.$axios.post("/sys/content/delete",
          contents
      ).then(res=>{
        this.$message({
          showClose: true,
          message: res.data.data,
          type: 'success',
          onClose:() => {
            this.findAngleByLexId(this.angleEditForm.lexId)
          }
        });
      })
    },

    findAngleNoLexId(){
      this.innerVisible = true
      this.$axios.get("/sys/angle/findAngleNoLexId",{
        params: {
          current: this.angleEditForm2.current,
          size: this.angleEditForm2.size,
          lexId: this.angleEditForm.lexId
        }
      }).then(res=>{
        this.angleData2= res.data.data.records
        this.angleEditForm2.size = res.data.data.size
        this.angleEditForm2.current = res.data.data.current
        this.angleEditForm2.total = res.data.data.total
      })

    },

    findAngleByLexId(lexId){
      this.angleEditForm.lexId = lexId
      this.outerVisible = true
      this.$axios.get("/sys/angle/findAngleByLexId",{
        params: {
          current: this.angleEditForm.current,
          size: this.angleEditForm.size,
          lexId: lexId
        }
      }).then(res => {
        this.angleData= res.data.data.records
        this.angleEditForm.size = res.data.data.size
        this.angleEditForm.current = res.data.data.current
        this.angleEditForm.total = res.data.data.total
      })


    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      console.log("勾选")
      console.log(val)
      this.multipleSelection = val;

      this.delBtlStatu = val.length == 0
    },

    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.size = val
      this.getLexiconList()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.current = val
      this.getLexiconList()
    },
    handleSizeChange2(val){
      this.angleEditForm.size = val
      this.findAngleByLexId()
    },
    handleCurrentChange2(val){
      this.angleEditForm.current = val
      this.findAngleByLexId()
    },
    handleCurrentChange3(val){
      this.angleEditForm2.current = val
      this.findAngleNoLexId()
    },

    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.dialogVisible = false
      this.editForm = {}
      this.dynamicTags=[]
    },
    handleClose() {
      this.resetForm('editForm')
    },

    getLexiconList() {
      this.$axios.get("/sys/lexicon/list", {
        params: {
          name: this.searchForm.name,
          current: this.current,
          size: this.size
        }
      }).then(res => {
        this.tableData = res.data.data.records
        this.size = res.data.data.size
        this.current = res.data.data.current
        this.total = res.data.data.total
      })
    },

    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var lexicon = {
              name:this.editForm.name,
              statu:this.editForm.statu,
              remark:this.editForm.remark,
              catelogNames:this.dynamicTags
             }
          if (this.editForm.id){
            lexicon = {
              id: this.editForm.id,
              name:this.editForm.name,
              statu:this.editForm.statu,
              remark:this.editForm.remark,
              catelogNames:this.dynamicTags
            }
          }
          console.log(lexicon)


          this.$axios.post('/sys/lexicon/' + (this.editForm.id?'update' : 'save'),lexicon)
              .then(res => {

                this.$message({
                  showClose: true,
                  message: '恭喜你，操作成功',
                  type: 'success',
                  onClose:() => {
                    this.getLexiconList()
                  }
                });

                this.dialogVisible = false
                this.resetForm(formName)
              })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    editHandle(id) {
      this.$axios.get('/sys/lexicon/info/' + id).then(res => {
        this.editForm = res.data.data
        this.dynamicTags = res.data.data.catelogNames
        this.dialogVisible = true
      })
    },
    delHandle(id) {

      var ids = []

      if (id) {
        ids.push(id)
      } else {
        this.multipleSelection.forEach(row => {
          ids.push(row.id)
        })
      }

      console.log(ids)

      this.$axios.post("/sys/lexicon/delete", ids).then(res => {
        this.$message({
          showClose: true,
          message: '恭喜你，操作成功',
          type: 'success',
          onClose:() => {
            this.getLexiconList()
          }
        });
      })
    },
    submitAngle(){
      var contents = []
      this.multipleSelection.forEach(row => {
        var content = {lexId:this.angleEditForm.lexId,aid:row.id}
        contents.push(content)
      })
      if (contents.length<1){
        alert("未选中")
        return;
      }else {
        this.$axios.post("/sys/content/addAngleLex",
            contents
        ).then(res => {
          this.$message({
            showClose: true,
            message: '关联角度成功',
            type: 'success',
            onClose:() => {
              this.findAngleNoLexId()
            }
          });
        })

      }

    },
    refresh(){
      this.current = 1
      this.size = 5
      this.searchForm.name = ''
      this.getLexiconList()
    },
    handleTagClose(tag) {
      this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
    },

    showInput() {
      this.inputVisible = true;
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },

    handleInputConfirm() {
      let inputValue = this.inputValue;
      if (inputValue) {
        this.dynamicTags.push(inputValue);
      }
      this.inputVisible = false;
      this.inputValue = '';
    }



  }

}
</script>

<style scoped>
.el-pagination {
  float: right;
  margin-top: 22px;
}



</style>
<style>
.el-popover{
  margin-left: 300px!important;
  margin-top: -70px!important;
}
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}

</style>
